var ModuleUtil = {
  configList: [{
    id: "materialName",
    label: "物料名称"
  }, {
    id: "materialModel",
    label: "物料规格"
  }, {
    id: "materialProviderName",
    label: "供应商"
  }, {
    id: "materialColor",
    label: "色号"
  }, {
    id: "materialColorDescription",
    label: "颜色描述"
  }],

  getMaterialTypeConfig: function () {
    return ajaxRequest('tableCoucmnlConfig/getConfigByName', {
      tableName: 'busniessOrderSetting'
    }).then(function (config) {
      var materialTypeConfig = {};
      if (config !== null) {
        var str = config.hideColumn;
        if (!SysUtil.isEmpty(str)) {
          materialTypeConfig = JSON.parse(str);
        }
      }
      return materialTypeConfig;
    });
  },

  saveMaterialTypeConfig: function (tableColObj) {
    return ajaxRequest('tableCoucmnlConfig/saveConfig', {
      tableName: 'busniessOrderSetting',
      hideColumn: JSON.stringify(tableColObj)
    }).then(function (msg) {
      return msg;
    });
  },

  getBomOrderList: function () {
    return ajaxRequest('createOrderForBomOrder/getBomOrderTitleList', {
      status: 1
    }).then(function (res) {
      var orderList = res.returnData;
      orderList = _.map(orderList, function (order) {
        var difficultLevel = order.difficultLevelRate;
        difficultLevel = Number(difficultLevel);
        return {
          id: order.id,
          factoryNo: order.factoryNo,
          serialNo: order.serialNo,
          difficultLevel: difficultLevel
        };
      });

      return orderList;
    });
  },

  getCustmerList:function(dropdownOption){
    var custmerList = dropdownOption.custmerList;
    if (custmerList !== undefined) {
      return Promise.resolve(custmerList);
    }

    return ajaxRequest('basicData/queryCustomersTow',{
      status:'使用'
    },false).then(function(res){
      var custmerList = res.returnData;
      var custmers = [];
      for (var i = 0; i < custmerList.length; i++) {
        var custmer = custmerList[i];
        var type = custmer.type;
        if ([2, 3].indexOf(type) !== -1) {
          custmers.push(custmer.customerName);
        }
      }
      dropdownOption.custmerList = custmers;
      return custmers;
    });
  },

  getBomOrderDetail: function (factoryNo) {
    return ajaxRequest('createOrderForBomOrder/getBomOrderById', {
      fullFactoryNo: factoryNo,
      status: 1
    }).then(function (res) {
      var materialList = [];
      if (res !== null) {
        materialList = res.materialList;
      }
      return materialList;
    });
  },

  getBusinessOrderDataById: function (order) {
    var applyId = order.applyId;
    var apiName, postData;
    if (SysUtil.isEmpty(applyId)) {
      postData = {
        id: order.id
      };
      apiName = 'createOrderForBomOrder/getBomOrderBussinessById';
    } else {
      postData = {
        applyId: applyId
      };
      apiName = 'createOrderForBomOrder/getBomOrderBussinessByIdForApply';
    }

    return ajaxRequest(apiName, postData).then(function (businessOrder) {
      return businessOrder;
    });
  },

  getBeforeUpdateBusinessOrder:function(order){
    return ajaxRequest('createOrderForBomOrder/getBomOrderBussinessById', {
      id: order.id
    }).then(function (businessOrder) {
      return businessOrder;
    });
  },

  approvalBusniessOrder: function (order, orderForm, status) {
    var applyId = orderForm.applyId;
    var apiName, postData;
    if (SysUtil.isEmpty(applyId)) {
      apiName = 'createOrderForBomOrder/updateBomOrderBusinessStatus';
      postData = {
        id: order.id,
        status: status
      };
    } else {
      if (status === 2) {
        status = 0;
      }
      apiName = 'createOrderForBomOrder/updateBomOrderBussinessForApply';
      postData = {
        id: applyId,
        status: status,
        serialNo: orderForm.serialNo
      };
    }

    return ajaxRequest(apiName, postData).then(function () {
      var msg = status === 1 ? '该商务订单审核通过' : '该商务订单成功退回';
      return msg;
    });
  }
};
